from audioop import reverse

from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login

# from .models import Person
from front11.forms import LoginForm


def index(request):
    # 创建普通用户
    # user = User.objects.create_user(username='zhiliao',email='zhiliao@qq.com',password='111111')
    # 创建超级用户    # userename必须是唯一的
    # user = User.objects.create_superuser(username='abc',email='abc@qq.com',password='111111')
    # 修改密码
    # user = User.objects.get(pk=1)
    # user.set_password('222222')
    # user.save()
    # 登陆验证
    username = 'zhiliao'
    password = '111111'
    user = authenticate(request,username=username,password=password)
    if user:
        return HttpResponse('登陆成功')
    else:
        return HttpResponse('登陆失败')

def proxy_view(request):
    # blacklist = Person.get_blacklist()
    # for person in blacklist:
    #     print(person.username)
    return HttpResponse("pro")

# 登陆验证
def my_authentioate(telephone,password):
    user = User.objects.filter(extension__telephone=telephone).first()
    if user:
        is_correct = user.check_password(password)
        if is_correct:
            return user
        else:
            return None
    else:
        return None


def one_view(request):
    ## 创建用户
    # user = User.objects.create_user(username='zhiliao',email='zhiliao@qq.com',password='111111')
    # user = User.objects.create_user(username='zhiliao2',email='zhiliao2@qq.com',password='111111')
    # user.extension.telephone = '18899997777'
    # user.save()
    # return HttpResponse('一对一扩展user模型')
    # 登陆验证
    telephone = request.GET.get('telephone')
    password = request.GET.get('password')
    user = my_authentioate(telephone,password)
    if user:
        return HttpResponse("验证成功")
    else:
        return HttpResponse("验证失败")

def inherit_view(request):
    # telephone = '18899997777'
    # password = '111111'
    # username = 'zhiliao1'
    # user = User.objects.create_superuser(telephone=telephone,username=username,password=password)
    # print(user.username)
    # user = authenticate(request,username='18899997777',password='111111')
    # if user:
    #     print('验证成功！')
    #     print(user.username)
    # else:
    #     print('验证失败！')


    User.objects.create_user(telephone='18899997777',password='111111',username='zhiliao')
    user = authenticate(request,username='18899997777',password='111111')
    if user:
        print(user.username)
        print('验证成功！')
    else:
        print('验证失败！')
    return HttpResponse('继承AbstractUser扩展用户')


# 切记：这里一定不要定义login视图函数
# 可以其他的名字
def my_login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        form = LoginForm(request.POST)
        if form.is_valid():
            telephone = form.cleaned_data.get('telephone')
            password = form.cleaned_data.get('password')
            remember = form.cleaned_data.get('remember')
            user = authenticate(request,username=telephone,password=password)
            if user and user.is_active:
                login(request,user)
                if remember:
                    # 设置为None，则表示使用全局的过期时间
                    request.session.set_expiry(None)
                else:
                    request.session.set_expiry(0)
                next_url = request.GET.get('next')
                if next_url:
                    return redirect(next_url)
                else:
                    return HttpResponse('登录成功')
            else:
                return HttpResponse('手机号码或者密码错误！')
        else:
            print(form.errors)
            return redirect(reverse('login'))